package com.jt.assistant.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.gengcon.www.jcprintersdk.JCAPI;
import com.jt.assistant.bean.CartonResponseBean;

public class B21PrinterHelper {


    public static void B21PrintData(JCAPI mPrinter, CartonResponseBean cartonGoods, boolean isSupplement){
        B21Print(mPrinter, cartonGoods, isSupplement);
        B21PrintGoods(mPrinter, cartonGoods);
        preparePrintBarcode(mPrinter, cartonGoods);
        mPrinter.endPage();
    }

    public static void printer(JCAPI mPrinter, String data) {
        mPrinter.startJob(50, 80, 0);
        mPrinter.startPage();
        mPrinter.drawText(data,
                1.5, 1.5,
                50, 80,
                5, 0.0,
                1.0F, (byte) 0x01, 0, 0, true, "");
        mPrinter.endPage();
    }


    @SuppressLint("DefaultLocale")
    public static void B21Print(JCAPI mPrinter, CartonResponseBean cartonGoods, boolean isSupplement) {
        if (cartonGoods.getCartonGoodsList().size() > 8) {
            mPrinter.startJob(50, 170, 0);
        } else {
            mPrinter.startJob(50, 80, 0);
        }

        mPrinter.startPage();
        mPrinter.drawText(String.format("%s 第%d箱", (isSupplement ?
                        cartonGoods.getReturnsCode() : cartonGoods.getChannelOrderNo()),
                Integer.parseInt(cartonGoods.getCartonCode().substring(12))),
                1.5, 1.5,
                50, 10,
                4, 0.0,
                1.0F, (byte) 0x01, 0, 0, true, "");

        String customerName = cartonGoods.getCustomerName();
        String storeName = cartonGoods.getStoreName();
        if (!TextUtils.isEmpty(customerName) && customerName.length() > 10) {
            customerName = customerName.substring(0, 10);
        }
        if (!TextUtils.isEmpty(storeName) && storeName.length() > 10) {
            storeName = storeName.substring(0, 10);
        }
        if (!TextUtils.isEmpty(customerName)) {
            mPrinter.drawText(customerName,
                    1.5, 8.5,
                    50, 10,
                    4, 0.0,
                    1.0F, (byte) 0x01, 0, 0, true, "");
        }
        if (!TextUtils.isEmpty(storeName)) {
            mPrinter.drawText(storeName,
                    1.5, 13.5,
                    50, 10,
                    4, 0.0,
                    1.0F, (byte) 0x01, 0, 0, true, "");
        }

        mPrinter.drawText("商品列表           SKU/数量",
                1.5, 18.5,
                50, 10,
                4, 0.0,
                1.0F, (byte) 0x01, 0, 0, true, "");

    }

    public static void preparePrintBarcode(JCAPI mPrinter, CartonResponseBean cartonGoods) {
        if (cartonGoods.getCartonGoodsList().size() > 8) {
            mPrinter.drawBarCode(cartonGoods.getCartonCode(), 20,
                    2, 150,
                    40, 15,
                    3, 1, 0, 0);
        } else {
            mPrinter.drawBarCode(cartonGoods.getCartonCode(), 20,
                    2, 60,
                    40, 15,
                    3, 1, 0, 0);
        }
    }

    public static void B21PrintGoods(JCAPI mPrinter, CartonResponseBean cartonGoods) {
        double height = 23.5;
        for (int i = 0; i < cartonGoods.getCartonGoodsList().size(); i++) {
            if (i == 12) height = height + 20;
            mPrinter.drawText(cartonGoods.getCartonGoodsList().get(i).getSku(),
                    1.5, height + (i * 4),
                    35, 10,
                    3, 0.0,
                    1.0F, (byte) 0x01, 0, 0, true, "");

            mPrinter.drawText(cartonGoods.getCartonGoodsList().get(i).getNum() + "",
                    35, height + (i * 4),
                    50, 10,
                    3, 0.0,
                    1.0F, (byte) 0x01, 0, 0, true, "");
        }
    }

    public static void printBarcode(JCAPI mPrinter, String jdCode, String cartonCode) {
        mPrinter.startJob(80, 50, 90);
        mPrinter.startPage();
        mPrinter.drawText(String.format("JD单号：%s", jdCode),
                10, 10,
                80, 10,
                4, 0.0,
                1.0F, (byte) 0x01, 0, 0, true, "");

        mPrinter.drawBarCode(cartonCode, 20,
                10, 20,
                60, 15,
                3, 1, 0, 0);
        mPrinter.endPage();
    }
}
